// pages/shopping/Econtract/Econtract.js
var app = getApp();
var esign_path= '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    look_icon: '../../../images/look_icon.png',
    agree_img: '../../../images/agree.png',
    contract_id: '', //合同id
    contractInfo: {},
    original_parts_html: '',
    equipments_info_html: '',
    isshow: false,
    load_show: false,
    imgArr: [],
    is_auth: '',
    sign_way: '',
    hiddenmodalput: true,
    email: '',
    emailfocus: false,
    h:'',
    authInfo: { 'status_name': '待认证', status: '' },
    show:false,
    buttons: [],
    url:''
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          h: res.windowHeight - 50
        })
      },
    })
    that.setData({
      contract_id: options.contract_id
      
    })
   
    that.getUserInfo();
    that.getRealnameInfo();
    that.getSealLists();
  },

  // onShow 
  onShow: function() {
    var that = this;
    that.getContractInfo();
  },

  // 获取合同签署方式
  getSystemConfig: function() {
    var that = this;
    app.ajax({
      url: 'Common/Common/getSystemConfig',
      data: {
        'key': 'sign_way',
        'user_id':that.data.contractInfo.rental_user_info.user_id
      },
      success: function(res) {
        var sign_way = 1;
        if (res.data.data == 2) {
          sign_way = 2;
        }
        that.setData({
          sign_way: sign_way
        })
      }
    })
  },

  //  签订合同
  bindAgree: function() {
    var that = this;
    var is_auth = that.data.is_auth;
    wx.navigateTo({
      url: '../../sign/sign?contract_id=' + that.data.contract_id,
    })
    return;
    if (is_auth == 1) {
      that.goRealname();
    } else {
      if (that.data.sign_way == 2) { //电子签名
        wx.navigateTo({
          url: '../../sign/sign?contract_id=' + that.data.contract_id,
        })
      } else { //确认同意
        app.ajax({
          url: 'User/Contract/sign',
          method: "POST",
          data: {
            contract_id: that.data.contract_id,
            contact_type: 1
          },
          success: function(res) {
            if (res.data.code == 1000) {
              that.setData({
                isshow: true
              })
              app.showToast(res.data.message);
              // wx.navigateBack({
              //   delta:1
              // })
            } else {
              app.showToast(res.data.message);
            }
          }
        })
      }
    }

  },
  //获取合同信息
  getContractInfo() {
    var that = this;
    app.ajax({
      url: 'User/Contract/getContractInfo',
      method: "POST",
      data: {
        contract_id: that.data.contract_id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          var contractInfo = res.data.data;
          var original_parts_html = "";
          if (contractInfo.original_parts.length > 0) {
            for (var i = 0; i < contractInfo.original_parts.length; i++) {
              original_parts_html += contractInfo.original_parts[i].parts_name +';';
            }
          }
          var equipments_info_html = '';
          // if (contractInfo.equipments_info.list.length > 0) {
          //   equipments_info_html += contractInfo.equipments_info.list[0].equipments_name + '(';
          //   for (var i = 0; i < contractInfo.equipments_info.list.length; i++) {
          //     equipments_info_html += contractInfo.equipments_info.list[i].equipments_number + ';';
          //   }
          //   equipments_info_html += ')';

          // }
          if (contractInfo.equipments_info.list.length > 0) {
            equipments_info_html = contractInfo.equipments_info.list[0].equipments_name ;
          }
          var status = contractInfo.contract_info.status;
          var isshow = false;
          if (status >= 4) {
            isshow = true;
          }
          that.setData({
            contractInfo: contractInfo,
            original_parts_html: original_parts_html,
            equipments_info_html: equipments_info_html,
            isshow: isshow
          })
          esign_path = contractInfo.contract_info.esign_path
          that.getSystemConfig();
       // that.getCompany(contractInfo.contract_info.company_id); // 获取入驻商信息
        } else {
          if (res.data.code == -1101) {
            app.showModal("", res.data.message, function() {
              var timer = setTimeout(function() {
               that.goRealname();
              }, 1000);
            });
          }
        }
        wx.hideLoading();
        that.setData({
          load_show: true
        })
      }
    })
  },

  // 获取入驻商信息
  getCompany: function(company_id) {
    var that = this;
    app.ajax({
      url: 'api/Company/getCompany',
      method: "POST",
      url_type: 1,
      data: {
        company_id: company_id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          var contractInfo = that.data.contractInfo;
          contractInfo.company_info = res.data.data;
          that.setData({
            contractInfo: contractInfo
          })
        }

      }
    })
  },

  //获取验证信息
  getAuthInfo: function() {
    var that = this;
    app.ajax({
      url: 'User/Realname/getAuthInfo',
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          var imgArr = [];
          if (res.data.data.auth_type == 1) {
            imgArr.push(res.data.data.front_idcard_pic_all);
            imgArr.push(res.data.data.back_idcard_pic_all);
          } else {
            imgArr.push(res.data.data.business_license_pic_all);
          }
          that.setData({
            imgArr: imgArr
          })
          wx.previewImage({
            current: imgArr[0], // 当前显示图片的http链接
            urls: imgArr // 需要预览的图片http链接列表
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  //获取用户信息
  getUserInfo: function() {
    var that = this;
    app.ajax({
      url: 'User/User/getUserInfo',
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.code == 1000) {
          var is_auth = res.data.data.is_auth;
          that.setData({
            is_auth: is_auth
          })
        } else {
          // app.showToast(res.data.message);
        }
      }
    })
  },
  // 发送至邮箱
  saveEcontract: function() {
    var that = this;
    // app.showToast('正在开发即将上线');
    // return;
    that.setData({
      hiddenmodalput: false,
      emailfocus: true
    })

  },
  // 邮箱地址输入
  emailInput: function(e) {
    var that = this;
    var email = e.detail.value;
    that.setData({
      email: email
    })
  },
  // 取消
  cancel: function(e) {
    var that = this;
    that.setData({
      email: '',
      hiddenmodalput: true,

    })

  },
  // 确认发送
  confirm: function(e) {
    var that = this;
    var contract_id = that.data.contract_id;
    var email = that.data.email;
    if (email == '') {
      app.showToast("请输入邮箱地址");
      return false;
    }
    wx.showLoading({
      title: '正在发送...',
    })
    app.ajax({
      url: 'Common/Email/saveContract',
      data: {
        contract_id: contract_id,
        email: email
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 1000) {
          app.showToast("邮件已发送，请注意查收");
          that.setData({
            hiddenmodalput: true,
            email: '',
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })

  },

  //跳转我的认证
  goRealname() {
    var that = this;
    if (that.data.authInfo.status == -2) {
      wx.navigateTo({ //../../shopping/uploadDocuments/uploadDocuments
        url: '../../shopping/uploadDocuments/uploadDocuments',
      })
    } else if (that.data.authInfo.status == -3) {
      wx.navigateTo({
        url: '../../shopping/faceRecognition/faceRecognition',
      })
    } else if (that.data.authInfo.status == 2 || that.data.authInfo.status == 1) {
      wx.navigateTo({
        url: '../../myCertification/myCertification',
      })
    } else {
      wx.navigateTo({
        url: '../../shopping/realName/realName',
      })
    }

  },
  //获取用户认证信息
  getRealnameInfo() {
    var that = this;
    app.ajax({
      url: "User/Realname/getAuthInfo",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            authInfo: res.data.data
          })
        } else {
          that.setData({
            authInfo: { 'status_name': '待认证', 'status': '' }
          })
        }
      }
    })
  },

  // 去首页
  gohome() {
    wx.switchTab({
      url: '../../home/home'
    });
  },
  
  //签章
  signSeal(){
   var that = this;
   that.setData({
     show:true
   })
    
  },
  getSealLists() {
    var that = this;
    app.ajax({
      url: 'User/Seal/getLists',
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          var arr = [];
           for(var i=0;i<res.data.data.length;i++){
             var item = {
               type: 'default',
               className: 'seal-item',
               text: res.data.data[i].name,
               value: res.data.data[i].seal_all_url
             };
             arr.push(item);
           }
         
          that.setData({
            buttons:arr
          })

        }
      }
    })
  },
  //选择签章
  buttontap(e) {
    var that = this;
    var desc = e.detail.item.text;
    var value = e.detail.item.value;
    app.showModal("", "是否确认选择" + desc,function(){
      wx.downloadFile({
        url: value,
        success: function (res) {
          that.fillPdf(res.tempFilePath);
          that.setData({
            url: res.tempFilePath
          })
        }
      });
      
    })
    
  },

  
  //填充PDF数据
  fillPdf: function (tempFilePath) {
    var that = this;
    wx.uploadFile({
      url: app.globalData._url + 'Common/Esignc/pdf',
      filePath: tempFilePath,
      name: 'file',
      header: {
        company: app.globalData.company_id,
        client: app.globalData.client,
      },
      formData: {
        'contract_id': that.data.contract_id
      },
      success: function (res) {
        wx.hideLoading();
        var data = JSON.parse(res.data);
        if (data.code == 1000) {
         that.signContract(data.data.filename, data.data.esign_user_sign);
        } else {
          that.setData({
            falg: false
          })
          app.showToast(data.message);
        }
      },
      fail: function (err) {

      }

    })
  },

  //签合同
  signContract: function (filename, esign_user_sign) {
    var that = this;
    app.ajax({
      url: 'User/Contract/sign',
      data: {
        contract_id: that.data.contract_id,
        contact_type: 2,
        esign_filename: filename,
        esign_user_sign: esign_user_sign
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          show: false
        })
        app.showToast(res.data.message);
        if (res.data.code == 1000) {
      
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/user/econtract/econtract?contract_id=' + that.data.contract_id,
            })
          }, 2000);
        }
      }
    })
  },

})